package com.zhs.base.config;

import com.zhs.base.config.properties.AMapProperties;
import com.zhs.base.config.properties.BaiduMapProperties;
import com.zhs.base.config.properties.TencentMapProperties;
import com.zhs.base.service.impl.AMapMapApiService;
import com.zhs.base.service.impl.BaiduMapApiService;
import com.zhs.base.service.impl.TencentMapApiService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;

/**
 * 核心配置类
 * @author Java架构师之旅
 * @version 1.0
 * @date 2022/8/16 17:06
 */
@Slf4j
@Configuration
@ConditionalOnClass({AMapMapApiService.class, BaiduMapApiService.class, TencentMapApiService.class})
@EnableConfigurationProperties({AMapProperties.class, BaiduMapProperties.class, TencentMapProperties.class})
public class MapApiConfigure {
    @Resource
    private AMapProperties aMapProperties;
    @Resource
    private BaiduMapProperties baiduMapProperties;
    @Resource
    private TencentMapProperties tencentMapProperties;

    @Bean("aMapMapApiService")
    @ConditionalOnMissingBean
    public AMapMapApiService aMapMapApiService() {
        final AMapMapApiService aMapMapApiService = new AMapMapApiService();
        if (null == aMapProperties || StringUtils.isBlank(aMapProperties.getAMapKey())){
            return null;
        }
        aMapMapApiService.setAMapProperties(aMapProperties);
        log.info("==========aMapMapApiService init===========");
        return aMapMapApiService;
    }

    @Bean("baiduMapApiService")
    @ConditionalOnMissingBean
    public BaiduMapApiService baiduMapApiService() {
        final BaiduMapApiService baiduMapApiService = new BaiduMapApiService();
        if (null == baiduMapProperties || StringUtils.isBlank(baiduMapProperties.getBaiduKey())){
            return null;
        }
        baiduMapApiService.setBaiduMapProperties(baiduMapProperties);
        log.info("==========baiduMapApiService init===========");
        return baiduMapApiService;
    }

    @Bean("tencentMapApiService")
    @ConditionalOnMissingBean
    public TencentMapApiService tencentMapApiService() {
        final TencentMapApiService tencentMapApiService = new TencentMapApiService();
        if (null == tencentMapProperties || StringUtils.isBlank(tencentMapProperties.getTencentKey())){
            return null;
        }
        tencentMapApiService.setTencentMapProperties(tencentMapProperties);
        log.info("==========tencentMapApiService init===========");
        return tencentMapApiService;
    }
}
